#drags from castle a to castle b
def drag_ft(castle,mycastle,debug = 0)
        max_drag = 180
                
        
        # set to 1 or higher for debug output
        debug = 0
        
        x_move = castle.getxpixel - mycastle.getxpixel
        y_move = castle.getypixel - mycastle.getypixel 
        
        dist = eukldistancepixel(castle,mycastle)
        

                
        #$browser.a(:title, $lang_map).click
        move = $browser.div(:id, "gameBoardContainer")
        
        #move.drag_and_drop_by(0,100)
        
        times       = (dist/max_drag).floor
        remaining   = dist.remainder(max_drag)
        
        
        if debug > 0
        puts 'drag to castle ' + castle.id + '    distance = ' + dist.to_s
        puts 'castle coords:  ' + castle.getxpixel.to_s + '/' + castle.getypixel.to_s
        puts 'mystle coords:  ' + mycastle.getxpixel.to_s + '/' + mycastle.getypixel.to_s
        puts 'move         :  ' + x_move.to_s + '/' + y_move.to_s
        puts 'times        :' + times.to_s + '  remaining : ' + remaining.to_s
        end
        
        x_normalized = x_move / dist
        y_normalized = y_move / dist
        
        if(dist > max_drag)
                for i in 0 ... times
                        move.drag_and_drop_by((-x_normalized*max_drag).to_i,(y_normalized*max_drag).to_i)
                        if debug > 0
                                puts i
                        end
                        wait(1,true,false)
                end
        end
        
        if debug > 0
        puts 'drag  remaining:' + (-x_normalized*remaining).to_s + '/' + (y_normalized*remaining).to_s
        end
        
        move.drag_and_drop_by((-x_normalized*remaining).to_i,(y_normalized*remaining).to_i)
        $drag_fin = true
end

def drag_to_tile_mid(castle,debug = 0)

		max_drag = 180
		
		move = $browser.div(:id, "gameBoardContainer")
		
		while true
			localx = castle.getxpixel - castle.tile_left
			localy = castle.getypixel - castle.tile_top

			mlocalx = $mycastle.getxpixel - $mycastle.tile_left
			mlocaly = $mycastle.getypixel - $mycastle.tile_top
			
			
			
			location = $browser.element(:id, "hab_" + castle.id).wd.location
		
			x_move = (location[0] - $mylocation[0] - localx + $tile_left_px/2).round
			y_move = (location[1] - $mylocation[1]).round
			
		if debug > 0
		puts castle.id
		puts 'mycastle x/y  coords  : ' + $mycastle.getxpixel.to_s   + ' / ' + $mycastle.getypixel.to_s
		puts 'mycastle x/y          : ' + $mycastle.getx.to_s   + ' / ' + $mycastle.gety.to_s
		puts '  castle x/y          : ' + castle.getx.to_s      + ' / ' + castle.gety.to_s
		puts 'postion on screen     : ' + location[0].to_i.to_s + ' / ' + location[1].to_i.to_s
		puts 'local coords castle   : ' + localx.to_s           + ' / ' + localy.to_s
		puts 'local coords mycastle : ' + mlocalx.to_s          + ' / ' + mlocaly.to_s
		puts '$tile_left/top_px     : ' + $tile_left_px.to_s    + ' / ' + $tile_top_px.to_s
		puts ' move                 : ' + x_move.to_s           + ' / ' + y_move.to_s
		end
			dist = Math.sqrt(x_move*x_move+y_move*y_move)
			
			if dist == 0
				break
			end
			
			if dist > max_drag
				factor = max_drag/dist
				x_move *= factor
				y_move *= factor
				move.drag_and_drop_by(-x_move.round,-y_move.round)
				sleep 1.0
			else
				move.drag_and_drop_by(-x_move,-y_move)
			end
			
		end
end

def drag_to_tile_mid2(castle,castle2,debug = 0)

		max_drag = 180
		
		move = $browser.div(:id, "gameBoardContainer")
		
		location2 = $browser.element(:id, "hab_" + castle2.id).wd.location
		
		while true
			localx = castle.getxpixel - castle.tile_left
			localy = castle.getypixel - castle.tile_top

			mlocalx = castle2.getxpixel - castle2.tile_left
			mlocaly = castle2.getypixel - castle2.tile_top
			
			
			
			location = $browser.element(:id, "hab_" + castle.id).wd.location
		
			x_move = (location[0] - location2[0] - localx + $tile_left_px/2).round
			y_move = (location[1] - location2[1]).round
			
		if debug > 0
		puts castle.id
		puts 'mycastle x/y  coords  : ' + castle2.getxpixel.to_s   + ' / ' + castle2.getypixel.to_s
		puts 'mycastle x/y          : ' + castle2.getx.to_s   + ' / ' + castle2.gety.to_s
		puts '  castle x/y          : ' + castle.getx.to_s      + ' / ' + castle.gety.to_s
		puts 'postion on screen     : ' + location[0].to_i.to_s + ' / ' + location[1].to_i.to_s
		puts 'local coords castle   : ' + localx.to_s           + ' / ' + localy.to_s
		puts 'local coords mycastle : ' + mlocalx.to_s          + ' / ' + mlocaly.to_s
		puts '$tile_left/top_px     : ' + $tile_left_px.to_s    + ' / ' + $tile_top_px.to_s
		puts ' move                 : ' + x_move.to_s           + ' / ' + y_move.to_s
		end
			dist = Math.sqrt(x_move*x_move+y_move*y_move)
			
			if dist == 0
				break
			end
			
			if dist > max_drag
				factor = max_drag/dist
				x_move *= factor
				y_move *= factor
				move.drag_and_drop_by(-x_move.round,-y_move.round)
				sleep 1.0
			else
				move.drag_and_drop_by(-x_move,-y_move)
			end
			
		end
end


def drag(castle,debug = 0)
		if hexdistance(castle,$mycastle) < 8
			drag_near(castle,debug)
		else
			drag_near(castle,debug)
		end
        $drag_fin = true
end


def drag_near(castle,debug = 0)
		max_drag = 180
		$browser.a(:title, $lang_map).click
		move = $browser.div(:id, "gameBoardContainer")
		
		while true
			localx = castle.getxpixel - castle.tile_left
			localy = castle.getypixel - castle.tile_top
			
			location = $browser.element(:id, "hab_" + castle.id).wd.location
		
			x_move = (location[0] - $mylocation[0]).round
			y_move = (location[1] - $mylocation[1]).round
			
		if debug > 0
		puts castle.id
		puts 'postion on screen     : ' + location[0].to_i.to_s + ' / ' + location[1].to_i.to_s
		puts 'local coords castle   : ' + localx.to_s           + ' / ' + localy.to_s
		puts '$tile_left/top_px     : ' + $tile_left_px.to_s    + ' / ' + $tile_top_px.to_s
		puts ' move                 : ' + x_move.to_s           + ' / ' + y_move.to_s
		end
			dist = Math.sqrt(x_move*x_move+y_move*y_move)
			
			if dist < 10
				break
			end
			
			if $browser.span(:text => "OK").present?
				puts "Clearing internal error"
				$browser.span(:text => "OK").click
			end
			
			if dist > max_drag
				factor = max_drag/dist
				x_move *= factor
				y_move *= factor
				move.drag_and_drop_by(-x_move.round,-y_move.round)
			else
				move.drag_and_drop_by(-x_move,-y_move)
			end
			
		end
end

def drag_far(castle,debug = 0)
		max_drag = 180
		$browser.a(:title, $lang_map).click
		
		$browser.image(:id, "wmapButtonImage").click

		while true
			unless $browser.element(:id, "bg_" + castle.globalx.to_s + "_" + castle.globaly.to_s).present?
				sleep 0.1
			else
				break
			end
		end
		
		move = $browser.div(:id, "gameBoardContainer")
		
		while true
			
			#location = $browser.element(:id, "hab_" + castle.id).wd.location
			#location = $browser.element(:id, "bg_16724_16098").wd.location
			location = $browser.element(:id, "bg_" + castle.globalx.to_s + "_" + castle.globaly.to_s).wd.location
			
		
			x_move = (location[0] - $mylocation[0]).round
			y_move = (location[1] - $mylocation[1]).round
			
		if debug > 0
		puts castle.id
		puts 'postion on screen     : ' + location[0].to_i.to_s + ' / ' + location[1].to_i.to_s
		#puts 'local coords castle   : ' + localx.to_s           + ' / ' + localy.to_s
		puts '$tile_left/top_px     : ' + $tile_left_px.to_s    + ' / ' + $tile_top_px.to_s
		puts ' move                 : ' + x_move.to_s           + ' / ' + y_move.to_s
		end
			dist = Math.sqrt(x_move*x_move+y_move*y_move)
			
			if dist == 0
				break
			end
			
			if $browser.span(:text => "OK").present?
				puts "Clearing internal error"
				$browser.span(:text => "OK").click
			end
			
			if dist > max_drag
				factor = max_drag/dist
				x_move *= factor
				y_move *= factor
				move.drag_and_drop_by(-x_move.round,-y_move.round)
			else
				move.drag_and_drop_by(-x_move,-y_move)
			end
			
		end
		
		$browser.image(:id, "wmapButtonImage").click
end